using System;
using System.Reflection;
using iPhOwa.Exchange.Attributes;

namespace iPhOwa.Exchange
{
	/// <summary>
	/// Helps work with enums.
	/// </summary>
	public static class EnumUtility
	{
		/// <summary>
		/// Get the <see cref="StringValueAttribute"/> of an enum.
		/// </summary>
		public static string GetStringValue(Enum value)
		{
			string result = string.Empty;

			if(value != null)
			{
				FieldInfo fi = value.GetType().GetField(value.ToString());

				if(fi != null)
				{
					StringValueAttribute[] attributes =
							(StringValueAttribute[])fi.GetCustomAttributes(typeof(StringValueAttribute), false);

					result = (attributes != null && attributes.Length > 0)
					         		? attributes[0].Value
					         		: value.ToString();
				}
			}

			return result;
		}
	}
}